package com.china_goldcard.horizontallistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;

/**
 * 默认的adapter
 */
public class DefaultHorizontalMenuAdapter implements HorizontalMenuAdapter<DefaultHorizontalMenuViewHolder,HorizontalMenuViewModel> {

    private Context context;
    private int itemW;
    private int itemH;
    private int resource;
    private float titleSize;

    public DefaultHorizontalMenuAdapter(Context context,int itemW, int itemH,int resource,float titleSize){
        this.context = context;
        this.itemH = itemH;
        this.itemW = itemW;
        this.resource = resource;
        this.titleSize = titleSize;
    }

    @Override
    public DefaultHorizontalMenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(resource,parent,false);
        ViewGroup.LayoutParams params = itemView.getLayoutParams();
        params.width = (int)itemW;
        params.height = (int)itemH;
        itemView.setLayoutParams(params);
        return new DefaultHorizontalMenuViewHolder(itemView,titleSize);
    }

    @Override
    public void onBindViewHolder(DefaultHorizontalMenuViewHolder holder, int position, HorizontalMenuViewModel model) {
        if (model.isFromNet()){
            Picasso.with(context)
                    .load(model.getImageUrl())
                    .placeholder(model.getPlaceHolder())
                    .into(holder.imageView);
        }else{
            holder.imageView.setImageDrawable(model.getIcon());
        }
        holder.textView.setText(model.getTitle());
    }
}
